package io.chenglicun.modules.data.service.statistics.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.common.utils.DateUtils;
import io.chenglicun.modules.data.dao.statistics.DataDayStatisticsDao;
import io.chenglicun.modules.data.dto.statistics.DataTimeStatisticsDTO;
import io.chenglicun.modules.data.dto.statistics.DataTotalStatisticsDTO;
import io.chenglicun.modules.data.entity.statistics.DataCategoryStatisticsEntity;
import io.chenglicun.modules.data.entity.statistics.DataDayStatisticsEntity;
import io.chenglicun.modules.data.enums.DataStatisticsTypeEnums;
import io.chenglicun.modules.data.param.DataDayStatisticsParam;
import io.chenglicun.modules.data.service.statistics.DataCategoryStatisticsService;
import io.chenglicun.modules.data.service.statistics.DataDayStatisticsService;
import liquibase.pro.packaged.L;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 总数据统计
 *
 * @author pipi 1151485299@qq.com
 * @since 1.0 2024-03-01
 */
@Service
public class DataTotalStatisticsServiceImpl  {

    public List<DataTotalStatisticsDTO> getTotal(){
        Map<String, List<DataCategoryStatisticsEntity>> categoryGroup = SimpleQuery.group(new LambdaQueryWrapper<DataCategoryStatisticsEntity>(), DataCategoryStatisticsEntity::getType);
        Map<String, List<DataDayStatisticsEntity>> timeGroup = SimpleQuery.group(new LambdaQueryWrapper<DataDayStatisticsEntity>(), DataDayStatisticsEntity::getType);
        ArrayList<DataTotalStatisticsDTO> totalStatisticsDTOS = new ArrayList<>();
        for (DataStatisticsTypeEnums value : DataStatisticsTypeEnums.values()) {
            DataTotalStatisticsDTO statisticsDTO = new DataTotalStatisticsDTO();
            statisticsDTO.setType(value.getValue());
            statisticsDTO.setName(value.getLable());
            if (categoryGroup.containsKey(value.getValue())){
                List<DataCategoryStatisticsEntity> entities = categoryGroup.get(value.getValue());
                Long longSum = entities.stream().map(DataCategoryStatisticsEntity::getDataNum).reduce(Long::sum).orElse(0L);
                statisticsDTO.setDataNum(longSum);
            }else if (timeGroup.containsKey(value.getValue())){
                List<DataDayStatisticsEntity> entities = timeGroup.get(value.getValue());
                Long longSum = entities.stream().map(DataDayStatisticsEntity::getDayData).reduce(Long::sum).orElse(0L);
                statisticsDTO.setDataNum(longSum);
            }else{
                statisticsDTO.setDataNum(0L);
            }
            totalStatisticsDTOS.add(statisticsDTO);
        }
        return totalStatisticsDTOS;
    }
}